#!/usr/bin/env bash
# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: chensili

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
LOG_DIR="${REPO_ROOT}/../log"

DATETIME="$(date +%Y%m%d-%H%M%S%z)"
LOG_FILE="log_download_s3_iosg_line.${DATETIME}"

mkdir -p "${LOG_DIR}"
cd "${REPO_ROOT}"
cd python

# source "${SCRIPT_DIR}/py_bootstrap.sh"
source ~/miniconda3/etc/profile.d/conda.sh 
conda activate coin2_motion_env

source ../../coin_deploy/support_feed_coin2/aws_key/feed.raw.coin2_reader.sh

MACHINES=(
    "feed-01.ap-northeast-1.aws.huobi"
    "feed-01.ap-southeast-1.aws"
    "feed-02.us-east-1.aws"
    "feed-02.eu-west-1.aws"
    "feed-02.ap-northeast-2.aws"
    "feed-05.ap-northeast-1.aws"
    "feed-05.cn-hongkong.aliyun"
    "feed-10.ap-northeast-1.aws"
)

if [ -n "${1}" ]; then
  TARGET_DATE="${1}"
else
  #TARGET_DATE="$(date --utc '+%Y%m%d')"
  #PRE_DATE="$(date --date=@$(( $(date +%s --date=${TARGET_DATE}) - 86400 )) '+%Y%m%d')"
  TARGET_DATE="$(date --utc --date='100 minutes ago' '+%Y%m%d')"
fi

BUCKET="feed.raw.coin2.corp.prestolabs.io"

# https://docs.aws.amazon.com/cli/latest/topic/s3-config.html
aws configure set default.s3.max_bandwidth 200MB/s

# https://github.com/aws/aws-cli/pull/2747
OPTIONS="--no-progress"

echo "BUCKET='${BUCKET}'"
date

#for machine in ${MACHINES[*]}
#do
#  if [ ! -n "${1}" ]; then
#    ./pyrunner coin/support/feed_archive/app/remove_s3_sync_tmp_files.py \
#      --machine=${machine} \
#      --date=${PRE_DATE}
#    SRC_DIR="s3://${BUCKET}/data/flow/${machine}/${PRE_DATE}"
#    DST_DIR="/export/iosg/coin/data/flow/${machine}/${PRE_DATE}"
#    echo ${SRC_DIR} ${DST_DIR}
#    aws s3 sync ${OPTIONS} "${SRC_DIR}" "${DST_DIR}" \
#        2>> "${LOG_DIR}/${LOG_FILE}"
#  fi
#done

for machine in ${MACHINES[*]}
do
  ./pyrunner coin/support/feed_archive/app/remove_s3_sync_tmp_files.py \
    --machine=${machine} \
    --date=${TARGET_DATE}
  SRC_DIR="s3://${BUCKET}/data/flow/${machine}/${TARGET_DATE}"
  DST_DIR="/export/iosg/coin-mirror-1/buckets/feed.raw.coin/${machine}/${TARGET_DATE}"
  echo ${SRC_DIR} ${DST_DIR}
  aws s3 sync ${OPTIONS} "${SRC_DIR}" "${DST_DIR}" \
      2>> "${LOG_DIR}/${LOG_FILE}"
done

date
